﻿<?php
	
	class Downfile{
	
		private $name;
		private $state;//批阅状况
		private $type;//文件类型
		private $dir;//dir	
		private $files;
		private $conn;
		
		function __construct(){
			$this->conn = conn();
		}	

		function getDate($date){
			 $str = explode(' ',$date);			 
			 return $str['0'];
		}

		//输出当前情况下所对应Name,Id,Zone,FromDate,ToDate情况的列表
		public function listAll(){			
			$query = "select * from I_Downfile";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['id'];
				$name = $row['name'];
				$state = $row['state'];
				$type = $row['type'];
				$date = $row['date'];
				$date = $this->getDate($date);
				$departments = $row['departments'];
				$item=array("id"=>$id,"type"=>$type,"state"=>$state,"name"=>$name,"date"=>$date,"departments"=>$departments);
				array_push($items, $item);
			}	
			return $items;			
		}

		public function upload($file){

			if(!is_dir('Doc'))
			{
			  mkdir('Doc');//路径若不存在则创建
			}
			$path = "./Doc/xiafawenjian/";
			$filepath = $path.$file['name']."pdf";
			$tmp_name = $file['tmp_name'];
			$showphpath = $path.$file['name'];
			
			$filepath = iconv("utf-8","gbk",$filepath);
			$showphpath = iconv("utf-8","gbk",$showphpath);
			var_dump($filepath);
			if($file['type'] != 'application/pdf')
			{
			   echo "文件类型不符pdf格式！";
			   die();
			}
			
			if(file_exists ($filepath))
			{
				unlink($filepath);
				echo "删除已存在的同名文件!";
			}
			
			if(move_uploaded_file($tmp_name,$showphpath))
				echo "上传成功！";		
			else
			{
				echo "文件上传失败！";
				die();
			}		
		}
		
		public function upload2($file){

			if(!is_dir('Doc'))
			{
			  mkdir('Doc');//路径若不存在则创建
			}
			$path = "./Doc/xiafawenjian/";
			$filepath = $path.$file['name']."jpg";
			$tmp_name = $file['tmp_name'];
			$showphpath = $path.$file['name'];
			
			$filepath = iconv("utf-8","gbk",$filepath);
			$showphpath = iconv("utf-8","gbk",$showphpath);

			if($file['type'] != 'image/jpeg')
			{
			   echo "文件类型不符jpg格式！";
			   die();
			}
			
			if(file_exists ($filepath))
			{
				unlink($filepath);
				echo "删除已存在的同名文件!";
			}
			
			if(move_uploaded_file($tmp_name,$showphpath))
				echo "上传成功！";		
			else
			{
				echo "文件上传失败！";
				die();
			}		
		}		
		public function insert($item){

			$name = $item['name'];
			$state = $item['state'];
			$type = $item['type'];
			$file = $item['file'];
			
			$departments = $item['departments'];
			$date = $item['date'];
			$file2 = $item['file2'];
			//dir 默认./Doc/xiafawenjian/
			
			$this->upload($file);
			$this->upload2($file2);
			
			$path = "./Doc/xiafawenjian/".$file['name'];
			$path2 = "./Doc/xiafawenjian/".$file2['name'];
			
			$query = "INSERT INTO I_Downfile(state,type,name,path,date,departments,response_path)
			VALUES('".$state."','".$type."','".$name."','".$path."','".$date."','".$departments."','".$path2."')";
			$stmt = $this->conn->query( $query );
		}	
		
		public function delete($id){
			$query = "delete from I_Downfile where id = '".$id."'";
			$stmt = $this->conn->query( $query );
		}
		
		public function getById($id){
			$query = "select * from I_Downfile where id = '".$id."'";
			$stmt = $this->conn->query( $query );
			$row = $stmt->fetch( PDO::FETCH_ASSOC );
			$id = $row['id'];
			$state = $row['state'];
			$type = $row['type'];
			$name = $row['name'];
			$path = $row['path'];
			$date = $row['date'];
			$date = $this->getDate($date);
			$departments = $row['departments'];
			$path2 = $row['response_path'];
			$item=array("id"=>$id,"state"=>$state,"type"=>$typt,"name"=>$name,
				"path"=>$path,"date"=>$date,"departments"=>$departments,"path2"=>$path2);
			return $item;			
		}
		
		public function update($item){
			$file = $item['file'];
			//dir 默认./Doc/xiafawenjian/
			$path;
			if($file['name'] != ''){
				$this->upload($file);
				$path = "./Doc/xiafawenjian/".$file['name'];
			}
			else{
				$i = $this->getById($item['id']);
				$path = $i['path'];
			}
			$file2 = $item['file2'];
			$path2;
			if($file2['name'] != ''){
				$this->upload2($file2);
				$path2 = "./Doc/xiafawenjian/".$file2['name'];
			}	
			else{
				$i = $this->getById($item['id']);
				$path2 = $i['path2'];
			}

			$id = $item['id'];
			$state = $item['state'];
			$type = $item['type'];
			$name = $item['name'];
			$departments = $item['departments'];
			$date = $item['date'];
			
			$query = "update I_Downfile SET name ='".$name."', state = '".$state."', type = '".$type."'
					, path = '".$path."' , response_path = '".$path2."' , departments = '".$departments."',
					date = '".$date."'  where id = '".$id."'";

			$stmt = $this->conn->query( $query );
		}		
		
	}
	
?>